package com.hyh.admin.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyh.ad.common.core.domain.AjaxResult;
import com.hyh.ad.common.core.domain.model.SysMenu;
import com.hyh.admin.sys.mapper.SysMenuMapper;
import com.hyh.admin.sys.service.ISysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class SysMenuServiceImpl implements ISysMenuService {
    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Override
    public List<SysMenu> selectListSysMenuPage(SysMenu sysMenu) {
        LambdaQueryWrapper<SysMenu> query = new LambdaQueryWrapper<>(sysMenu);
        query.orderByAsc(SysMenu::getCreateTime);
        return sysMenuMapper.selectList(query);
    }

    @Override
    public SysMenu selectSysMenuById(Long id) {
        return sysMenuMapper.selectById(id);
    }

    @Override
    public int insertSysMenu(SysMenu sysMenu) {
        return sysMenuMapper.insert(sysMenu);
    }

    @Override
    public int updateSysMenu(SysMenu sysMenu) {
        return sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public int deleteSysMenuByIds(Long[] ids) {
        return sysMenuMapper.deleteBatchIds(Collections.singletonList(ids));
    }

    @Override
    public AjaxResult selectSysMenuTree(Long parentId) {
        LambdaQueryWrapper<SysMenu> query = new LambdaQueryWrapper<>();
        query.eq(SysMenu::getParentId, parentId);
        List<SysMenu> sysMenus = sysMenuMapper.selectList(query);
        return  AjaxResult.success(sysMenuMapper.selectList(query));
    }

    @Override
    public AjaxResult selectByMenuName(String menuName) {
        return AjaxResult.success(sysMenuMapper.selectList(new LambdaQueryWrapper<SysMenu>().like(SysMenu::getMenuName, menuName)));
    }
}
